"""
自定义异常类
"""
from enum import Enum


class ErrorType(Enum):
    """错误类型枚举"""
    API_ERROR = "API_ERROR"
    GENERATION_ERROR = "GENERATION_ERROR"
    UPLOAD_ERROR = "UPLOAD_ERROR"
    VALIDATION_ERROR = "VALIDATION_ERROR"
    NETWORK_ERROR = "NETWORK_ERROR"
    AUTH_ERROR = "AUTH_ERROR"
    DATABASE_ERROR = "DATABASE_ERROR"


class ErrorCode(Enum):
    """错误代码枚举"""
    DEEPSEEK_QUOTA_EXCEEDED = "DS001"
    DEEPSEEK_RATE_LIMITED = "DS002"
    TONGYI_RATE_LIMITED = "TY001"
    TONGYI_QUOTA_EXCEEDED = "TY002"
    WECHAT_TOKEN_EXPIRED = "WX001"
    WECHAT_UPLOAD_FAILED = "WX002"
    CONTENT_TOO_SHORT = "CT001"
    CONTENT_TOO_LONG = "CT002"
    IMAGE_UPLOAD_FAILED = "IM001"
    IMAGE_GENERATION_FAILED = "IM002"
    DATABASE_CONNECTION_ERROR = "DB001"
    MYSQL_QUERY_ERROR = "DB002"
    THEME_NOT_FOUND = "TH001"
    ARTICLE_NOT_FOUND = "AR001"


class BaseCustomException(Exception):
    """基础自定义异常"""
    
    def __init__(self, message: str, error_type: ErrorType = None, error_code: ErrorCode = None):
        self.message = message
        self.error_type = error_type
        self.error_code = error_code
        super().__init__(message)


class ContentGenerationError(BaseCustomException):
    """内容生成错误"""
    
    def __init__(self, message: str, error_code: ErrorCode = None):
        super().__init__(message, ErrorType.GENERATION_ERROR, error_code)


class ImageGenerationError(BaseCustomException):
    """图像生成错误"""
    
    def __init__(self, message: str, error_code: ErrorCode = None):
        super().__init__(message, ErrorType.GENERATION_ERROR, error_code)


class WeChatAPIError(BaseCustomException):
    """微信API错误"""
    
    def __init__(self, message: str, error_code: ErrorCode = None):
        super().__init__(message, ErrorType.API_ERROR, error_code)


class DatabaseError(BaseCustomException):
    """数据库错误"""
    
    def __init__(self, message: str, error_code: ErrorCode = None):
        super().__init__(message, ErrorType.DATABASE_ERROR, error_code)


class ValidationError(BaseCustomException):
    """验证错误"""
    
    def __init__(self, message: str, error_code: ErrorCode = None):
        super().__init__(message, ErrorType.VALIDATION_ERROR, error_code)


class ThemeNotFoundError(BaseCustomException):
    """主题不存在错误"""
    
    def __init__(self, theme_id: str):
        message = f"主题不存在: {theme_id}"
        super().__init__(message, ErrorType.VALIDATION_ERROR, ErrorCode.THEME_NOT_FOUND)


class ArticleNotFoundError(BaseCustomException):
    """文章不存在错误"""
    
    def __init__(self, article_id: str):
        message = f"文章不存在: {article_id}"
        super().__init__(message, ErrorType.VALIDATION_ERROR, ErrorCode.ARTICLE_NOT_FOUND)